// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: ziyan

#pragma once

#include <string>

#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>

#include "coin/proto/coin_market_enums.pb.h"
#include "coin2/base/conversion.h"
#include "coin2/exchange/base/feed/update.h"
#include "coin2/exchange/base/feed_util/book_builder.h"
#include "coin2/exchange/base/feed_util/trade.h"

namespace impl {
using coin2::exchange::base::market::ExchangeType;
using coin2::exchange::base::market::MarketType;

class BitmartFeedUpdateParser {
 public:
  static void ParseBook(BookBuilder* book_builder_, const rapidjson::Value& book_json) {
    auto ts = book_json["ms_t"].GetInt64() * 1'000'000LL;
    auto tx = book_builder_->NewTransaction(ts);
    tx.Clear();
    for (const auto& pq : book_json["bids"].GetArray()) {
      tx.UpdateBid(stod(pq[0].GetString()), stod(pq[1].GetString()));
    }
    for (const auto& pq : book_json["asks"].GetArray()) {
      tx.UpdateAsk(stod(pq[0].GetString()), stod(pq[1].GetString()));
    }
  }

  static void ParseTrade(const rapidjson::Value& trade_json, Trade* trade) {
    trade->timestamp = trade_json["s_t"].GetInt64() * 1'000'000'000LL;
    trade->price = stod(trade_json["price"].GetString());
    trade->fill_qty = stod(trade_json["size"].GetString());
    std::string side = trade_json["side"].GetString();
    trade->side = ParseTradeSide(side);
  }

  static coin2::exchange::feed::TradeSide ParseTradeSide(const std::string& side) {
    if (side == "buy") {
      return coin2::exchange::feed::TRADE_BUY_SIDE;
    } else if (side == "sell") {
      return coin2::exchange::feed::TRADE_SELL_SIDE;
    } else {
      throw std::runtime_error("[Bitmart] unknown side: " + side);
    }
  }

  static fastfeed::proto::KlineInterval ParseKlineInterval(std::string_view interval) {
    static std::map<std::string, fastfeed::proto::KlineInterval> interval_map = {
        {"1", fastfeed::proto::KLINE_INTERVAL_1MIN},
        {"5", fastfeed::proto::KLINE_INTERVAL_5MIN},
        {"15", fastfeed::proto::KLINE_INTERVAL_15MIN},
        {"30", fastfeed::proto::KLINE_INTERVAL_30MIN},
        {"60", fastfeed::proto::KLINE_INTERVAL_1HOUR},
        {"120", fastfeed::proto::KLINE_INTERVAL_2HOUR},
        {"240", fastfeed::proto::KLINE_INTERVAL_4HOUR},
        {"1440", fastfeed::proto::KLINE_INTERVAL_1DAY},
        {"10080", fastfeed::proto::KLINE_INTERVAL_1WEEK},
        {"43200", fastfeed::proto::KLINE_INTERVAL_1MON}};

    auto iter = interval_map.find(std::string(interval));
    if (iter == interval_map.end()) {
      return fastfeed::proto::KLINE_INTERVAL_INVALID;
    }
    return iter->second;
  }

  static bool ParseKlineRest(
      const rapidjson::Value& msg,
      Klines* klines,
      fastfeed::proto::KlineInterval interval,
      int64_t timestamp) {
    int64_t open_timestamp = msg["timestamp"].GetInt64() * 1'000'000'000LL;
    auto result = klines->AddKline(open_timestamp);
    if (!result) return false;

    Kline* kline;
    bool freezed;
    std::tie(kline, freezed) = result.value();
    kline->open_timestamp = open_timestamp;
    kline->open = stod(msg["open"].GetString());
    kline->high = stod(msg["high"].GetString());
    kline->low = stod(msg["low"].GetString());
    kline->close = stod(msg["close"].GetString());
    kline->volume = stod(msg["volume"].GetString());
    kline->turnover = stod(msg["quote_volume"].GetString());
    kline->timestamp = timestamp;
    kline->interval = interval;
    
    return freezed;
  }
};

}  // namespace impl

using impl::BitmartFeedUpdateParser;